package com.zq.it.dao.impl.admin;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.zq.it.dao.admin.StudentDao;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.Student;

public class StudentDaoImpl extends BaseDao<Student> implements StudentDao {

	/* public static final Logger logger=t(StudentDaoImpl.class); */
	private static final org.slf4j.Logger log = LoggerFactory
			.getLogger(StudentDaoImpl.class);
	// property constants
	public static final String STUDENT_NO = "studentNo";
	public static final String STUDENT_NAME = "studentName";
	public static final String SEX = "sex";
	public static final String STUDENT_IMG = "studentImg";
	public static final String IDCARD_NO = "idcardNo";
	public static final String MOBILE = "mobile";
	public static final String CLASS_ID = "classId";
	public static final String EMAIL = "email";
	public static final String CREATE_TIME = "createTime";
	public static final String EXTEND_FIELD = "extendField";

	@Override
	public void saveStudentEty(Student student) {
		getHibernateTemplate().save(student);
	}
	@Override
	public void deleteStudentEtyByID(Integer id) {
		deleteEntity("delete Student as s where s.stuId=" + id);
	}

	@Override
	public List findStudentEtyList() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createCriteria(Student.class)
						.setFetchMode("tclass", org.hibernate.FetchMode.JOIN)
						.list();
			}

		});
	}

	@Override
	public List findStudentEtyById(final long id) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {

				return arg0.createCriteria(Student.class)
						.add(Restrictions.eq("stuId", id))
						.setFetchMode("tclass", org.hibernate.FetchMode.JOIN)
						.list();
			}

		});
	}

	@Override
	public List findStudentEtyByName(final String name) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				return arg0.createCriteria(Student.class)
						.add(org.hibernate.criterion.Restrictions
								.like("stuName", name))
						.setFetchMode("tlcass", org.hibernate.FetchMode.JOIN)
						.list();
			}
		});
	}

	// @Override
	// public void updateStudentByID(Integer id) {
	// getHibernateTemplate().update(entity);
	// // TODO Auto-generated method stub
	//
	// }

	@Override
	public void updateStudentEty(Student student) {
		getHibernateTemplate().update(student);
		// TODO Auto-generated method stub

	}
	// @Override
	// public List findStudentEtyByDate(Date date){
	// // TODO Auto-generated method stub
	// return getHibernateTemplate().find("from Student where
	// createTime=?",date);
	// }
	@Override
	public List findStudentEtyByExmp(Student student) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByExample(student);
	}
	@Override
	public void deleteAllEty(long[] ids) {

		org.springframework.orm.hibernate3.HibernateTemplate ht = getHibernateTemplate();
		for (long i : ids)
			ht.delete(ht.load(Student.class, i));
	}
	@Override
	public void deleteStudentEtyByObj(Student student) {
		getHibernateTemplate().delete(student);
	}
	@Override
	public void saveStudentEtyList(Collection<Student> collection) {
		getHibernateTemplate().saveOrUpdateAll(collection);
	}
	@Override
	public List findStudentEtyByHql(String hql) {
		// TODO Auto-generated method stub
		return this.findEtyByHql(hql);
	}

	@Override
	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public Student findById(Long id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) getHibernateTemplate().get("com.zq.it.pojo.Student", id);			
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Student as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public List findByTclass(Object value) {
		log.debug("finding Student instance with property: ");
		try {
			String queryString = "from Student as model where model.tclass.classId=?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public List findByStudentNo(Object studentNo) {
		return findByProperty(STUDENT_NO, studentNo);
	}

	@Override
	public List findByStudentName(Object studentName) {
		return findByProperty(STUDENT_NAME, studentName);
	}

	@Override
	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	@Override
	public List findByStudentImg(Object studentImg) {
		return findByProperty(STUDENT_IMG, studentImg);
	}

	@Override
	public List findByIdcardNo(Object idcardNo) {
		return findByProperty(IDCARD_NO, idcardNo);
	}

	@Override
	public List findByMobile(Object mobile) {
		return findByProperty(MOBILE, mobile);
	}

	@Override
	public List findByClassId(Object classId) {
		return findByTclass(classId);
	}

	@Override
	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	@Override
	public List findByCreateTime(Object createTime) {
		return findByProperty(CREATE_TIME, createTime);
	}

	@Override
	public List findByExtendField(Object extendField) {
		return findByProperty(EXTEND_FIELD, extendField);
	}

	@Override
	public List findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from Student";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@Override
	public void update(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public List findByHql(String hql) {
		log.debug("finding Student instances by Hql");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find by Hql failed", re);
			throw re;
		}
	}
}
